(function() {
	var $window = $(window);
	var available_filed = $window.height();
	var _mark;

	var get_goods_type;
	var get_goods_tag;
	var get_goods_sort_key;
	var get_goods_sort_asc;
	var goods_pre_num = 24;
	var current_page_num = 0;
	var goods_list_url;
	// 判断位置自动加载更多商品
	$window.stop().scroll(function() {
		if (_mark && !App.get("loadingoods") && ((window.scrollY + available_filed) > _mark.offsetTop)) {
			App.get("$Event.goods_list.getMoreGoods")();
		};
	});
	// 获取更多商品
	App.set('$Event.goods_list.getMoreGoods', function() {
		if (App.get('isresult')) {
			App.set('loadingoods', true);
			current_page_num += 1;
			getgoods();
		};
	});


	//获取指定页的商品
	function getgoods() {
		var data = {
			bus_id: busInfo._id,
			num: goods_pre_num,
			page: current_page_num
		};
		(get_goods_sort_key && (get_goods_type != 2)) ? data.sort_by_key = "cash": false;
		((get_goods_sort_asc == 1) && (get_goods_type != 2)) ? data.asc = true: false;
		App.set('loadingoods', true);
		_get_goods_async(data).then(function(list_data) {
			_mark = document.getElementById("_mark");
		});
	};

	function _get_goods_async(data) {
		return new Promise(function(resolve, reject) {
			coAjax.get(goods_list_url, data, function(result) {
				if (result.result.length) {
					App.set('isresult', true);
					App.concat('$Cache.goods_list.list', result.result);
				} else {
					App.set('isresult', false);
				};
				App.set('loadingoods', false);
				resolve(result);
			});
		});
	};

	Path.on("/__basename__.html", function(_current_location) {
		var queryString = _current_location.query;
		get_goods_type = queryString.get("id");
		get_goods_tag = queryString.get("tag");
		get_goods_sort_key = queryString.get("sort");
		get_goods_sort_asc = queryString.get("asc");
		App.set('$Cache.goods_list.list', []);
		current_page_num = 0;
		App.set("$Cache.goods_list.sort_asc", get_goods_sort_asc);
		(get_goods_sort_asc == 0) ? App.set("$Cache.goods_list.sort_asc", true): App.set("$Cache.goods_list.sort_asc", false);
		App.set("$Event.goods_list.change_sort_asc", function() {
			(get_goods_sort_asc == 0) ? Path.jump("/goods_list.html?sort=cash&asc=1"): Path.jump("/goods_list.html?sort=cash&asc=0");
		});
		App.set("$Cache.goods_list.type", get_goods_type);
		App.set("$Cache.current_page", 2);
		//获取所有商品数量
		coAjax.get(appConfig.goods.number, {
			bus_id: busInfo._id,
		}, function(result) {
			var number = ~~result.result
			App.set("$Cache.goods_number", number);
			var _goods_number_list = [];
			_goods_number_list.length = number;
			var _page_num = [];
			//向上取整获取页数
			// console.log(number, goods_pre_num, Math.ceil(number / goods_pre_num));
			_page_num.length = Math.ceil(number / goods_pre_num);
			App.set("$Cache.goods_number_list", _goods_number_list);
			App.set("$Cache.page_num", _page_num);
		});


		// 判断排序
		if (get_goods_type == 2) {
			goods_list_url = appConfig.goods.quality_list;
			App.set('$Cache.goods_list.typetitle', '销量最高');
			App.set('$Cache.goods_list.list', []);
		} else {
			goods_list_url = appConfig.goods.list;
			App.set('$Cache.goods_list.typetitle', '最新上架');
			(get_goods_sort_asc == 0) ? App.set("$Cache.goods_list.typetitle", "价格从低到高"): false;
			(get_goods_sort_asc == 1) ? App.set("$Cache.goods_list.typetitle", "价格从高到低"): false;
			App.set('$Cache.goods_list.list', []);
		};
		var data = {
			bus_id: busInfo._id,
			num: goods_pre_num,
			page: current_page_num
		};
		console.log(data)
		(get_goods_sort_key && (get_goods_type != 2)) ? data.sort_by_key = "cash": false;
		((get_goods_sort_asc == 1) && (get_goods_type != 2)) ? data.asc = true: false;
		App.set('loadingoods', true);
		coAjax.get(goods_list_url, data, function(result) {
			if (result.result.length) {
				App.set('isresult', true);
				App.set('$Cache.goods_list.list', result.result);
			} else {
				App.set('isresult', false);
			};
			App.set('loadingoods', false);
		});
		//每页显示数量
		App.set("config.goods_pre_num", goods_pre_num);
		App.set("$Cache.goods_pre_num", goods_pre_num);
		jSouper.ready(function(argument) {
			App.set("$Cache.current_page_num", current_page_num);
		});

	});

	// 返回顶部
	var $body = $(document.body);
	App.set("$Event.goods_list.back_to_top", function() {
		$body.stop().animate({
			scrollTop: 0
		}, 300);
	});
}());